const {baseController} = require('../../baseController')
const serviceEnter = require('../../../service/serviceEnter')
const { result } = require('../../../util/result/result')


class doTitleController extends  baseController{

    static sigleIns = new doTitleController()


    constructor(){
        super(serviceEnter.doTitleService.getIns())
    }

    

    static getSigleIns =()=>{
        if(this.sigleIns){
            return this.sigleIns
        }else{
            return new doTitleController()
        }
    }


    async findByObjAndUpdate (req,res){

        let {userid,tittleid,dolog} =req.body



        let data
        let findres
        let gogo 

        let conditions = {"userid":userid,"tittleid":tittleid}
        let update = req.body
        // let options =  {"upsert":true, "new":true, "setDefaultsOnInsert": true}
            let options =  {"upsert":true}

            try {
                findres =  await this.service.findOne(conditions,update,options)
              } catch (error) {
                  res.send(result(false,error))
                  console.log(error);
                  return
              }

        if(findres!=null){
                    try {
                        data =  await  this.service.findByObjAndUpdateArray({"userid":userid,"tittleid":tittleid},{"dolog":dolog})
                    } catch (error) {
                        res.send(result(false,error))
                        return
                    }
                    if(data&&data!=undefined){
                        res.send(result(true,data))
                    }else{
                        res.send(result(false))
                    }
        }else {
                    try {
                    gogo =     await this.service.create(req.body)
                    } catch (error) {
                        res.send(result(false,error))
                        console.log(error);
                        return
                    }

                    if(gogo==undefined){
                        res.send(result(false))
                    }else{
                        res.send(result(true,gogo))
                    }

        }




       }


}

module.exports={
    doTitleController
}